package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.MessageAddReq;
import com.zucc.controller.req.MessageModifyReq;
import com.zucc.controller.req.MessageSearchReq;
import com.zucc.controller.resp.MessageResp;
import com.zucc.entity.Message;
import com.zucc.entity.MessageMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.MessageService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-05 4:29 下午
 */
@Service
public class MessageServiceImpl extends MyServiceImpl<Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Override
    public MyMapper<Message> mapper() {
        return messageMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MessageAddReq req) {
        Message message = new Message();
        BeanUtil.copyProperties(req, message);
        message.setDelFlag(0);
        message.setCreateTime(DateUtil.date());
        message.setModifyTime(DateUtil.date());
        super.saveKey(message);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(MessageModifyReq req) {
        Message messageExist = super.findById(req.getId());
        if (Objects.isNull(messageExist) || messageExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的留言信息");
        }
        BeanUtil.copyProperties(req, messageExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        messageExist.setModifyTime(DateUtil.date());
        super.updateSelective(messageExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        Message messageExist = super.findById(id);
        if (Objects.isNull(messageExist) || messageExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的留言信息");
        }
        messageExist.setDelFlag(1);
        messageExist.setDeleteTime(DateUtil.date());
        super.updateSelective(messageExist);
    }

    @Override
    public PageData<MessageResp> page(MessageSearchReq req) {
        Example example = new Example(Message.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);
        if (Objects.nonNull(req.getSysTypeDictItemId())) {
            criteria.andEqualTo("sysTypeDictItemId", req.getSysTypeDictItemId());
        }
        if (Objects.nonNull(req.getUserId())) {
            criteria.andEqualTo("userId", req.getUserId());
        }
        if (StrUtil.isNotBlank(req.getRemarks())) {
            criteria.andLike("remarks", "%" + req.getRemarks() + "%");
        }
        if (StrUtil.isNotBlank(req.getContent())) {
            criteria.andLike("content", "%" + req.getContent() + "%");
        }
        if (StrUtil.isNotBlank(req.getContact())) {
            criteria.andLike("contact", "%" + req.getContact() + "%");
        }
        PageData<Message> pageData = super.findByPage(req, example);
        PageData<MessageResp> convert = MyPageUtil.convert(pageData, MessageResp.class);
        return convert;
    }
}
